package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AccountLockedException;
import com.sky.exception.AccountNotFoundException;
import com.sky.exception.PasswordErrorException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 员工登录
     *
     * @param employeeLoginDTO
     * @return
     */
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        if (!password.equals(employee.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        //3、返回实体对象
        return employee;
    }

    /**
     * 员工添加
     *
     * @param employeeDTO
     * @return
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        String password = DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes());
        employee.setPassword(password);
        employee.setStatus(StatusConstant.ENABLE);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long currentId = BaseContext.getCurrentId();
        employee.setCreateUser(33L);//把创建人id 写死
        employee.setUpdateUser(33L);//把修改人id 写死
        employeeMapper.insert(employee);//Mapper
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        //介入分页操作
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        //做基础查询
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> records = page.getResult();

        return new PageResult(total, records);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //员工 要完成修改1根据id修改状态
        // 员工 要完成修改2根据id修改其他属性也就是 具体修改什么属性,其实是不具体的 是动态的 所以 这里可以采用动态sql形式完成动态更新 <set><if>
        // 两个修改操作合二为一呢 定义一个对象 用来封装被修改的属性信息
        Employee updateEmp = new Employee();
        //用来封装 id 条件
        updateEmp.setId(id);
        updateEmp.setStatus(status);
        employeeMapper.update(updateEmp);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        //employee 有密码 --密码不适合返回
        employee.setPassword("******");

        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        // 更新操作 employeeDTO 存储的是前端传递的修改的基本信息
        //  我们还有两个字段需要完成更新
        // update_time 记录更新时间 update_user 记录更新人
        //定义 Employee对象 用来封装需要 在数据库更新的数据
        Employee employee = new Employee();
        //把基本属性进行复制
        BeanUtils.copyProperties(employeeDTO,employee);
        //完成 更新时间和更新人的设置
        employee.setUpdateTime(LocalDateTime.now());
        //更新人
        Long currentId = BaseContext.getCurrentId();
        employee.setUpdateUser(currentId);

        //所有的更新信息都封装到 employee中
        employeeMapper.update(employee);
    }






}